package com.theaf.basemodule.common.dto;

import com.theaf.basemodule.common.api.AreaService;
import com.theaf.basemodule.common.model.SysArea;
import com.theaf.core.utils.BeanMapper;
import com.theaf.core.utils.DictionaryHelper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@ApiModel("地区DTO")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AreaDTO implements Serializable {
    public static String TYPE_PROVINCE="1";//省
    public static String TYPE_CITY="2";//市
    public static String TYPE_COUNTRY = "0";//国家;

    @ApiModelProperty("地区ID")
    private long id;
    @ApiModelProperty("父地区ID")
    private Long pid;
    @ApiModelProperty("父地区名称")
    private String pName;
    @ApiModelProperty("地区名称")
    private String name;
    @ApiModelProperty("地区编码")
    private String code;
    @ApiModelProperty("地区类型")
    private String type;
    private String typeName;
    @ApiModelProperty("邮编")
    private String zip;
    private int level;

    /**
     * 由PO数据转换成DTO数据
     * @param sysArea
     * @return
     */
    public static AreaDTO fromPo(SysArea sysArea){
        AreaDTO areaDTO = BeanMapper.map(sysArea,AreaDTO.class);
        areaDTO.setTypeName(DictionaryHelper.getChlidValue(AreaService.TYPE_DIC_NAME,sysArea.getType()));
        if(sysArea.getParent()!=null) {
            areaDTO.setPid(sysArea.getParent().getId());
            areaDTO.setPName(sysArea.getParent().getName());
        }
        areaDTO.setLevel(sysArea.getLevelCode().length());
        return areaDTO;
    }
}
